package cn.kgc.crowdfundingadminspringboot2.controller;

import cn.kgc.crowdfundingadminspringboot2.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadminspringboot2.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot2.pojo.Admin;
import cn.kgc.crowdfundingadminspringboot2.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    AdminService adminService;

    @RequestMapping("/to/login/page.html")
    public String test01(Integer id){
        return "admin-login";
    }

    @RequestMapping("/do/login")
    public String doLogin(String loginAcct,
                          String userPswd,
                          HttpSession session,
                          Model model){
        Admin admin = adminService.login(loginAcct,userPswd);
        if(admin == null){
            //登录失败
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE,CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }

        session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN,admin);
        return "redirect:/admin/to/main/page.html";
    }
    //重定向登录
    @RequestMapping("/to/main/page.html")
    public String doMain(){
        return "admin-main";
    }

    //退出
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";
    }

    //异常处理
    @ExceptionHandler(value=Exception.class)
    public String catchException(Exception exception,Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception.getMessage());
        return "system-error";
    }
    //分页
    @RequestMapping("/query/for/search")
    public String queryForSearch(

            // 如果页面上没有提供对应的请求参数，那么可以使用defaultValue指定默认值
            @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
            @RequestParam(value="keyword", defaultValue="") String keyword,
            Model model) {

        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);

        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);

        System.out.println(pageInfo.getPageNum());

        for (Admin admin : pageInfo.getList()) {
            System.out.println(admin.getUsername());
        }

        return "admin-page";
    }

    /**
     * 删除
     * @param adminIdList
     * @return
     */
    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {

        try {

            adminService.batchRemove(adminIdList);

            return ResultEntity.successWithoutData();
        }catch(Exception e) {

            return ResultEntity.failed(null, e.getMessage());
        }

    }

    //跳转添加页面
    @RequestMapping("/to/add/page.html")
    public String adminadd(){
        return "admin-add";
    }

    //添加用户
    @RequestMapping("/save")
    public String saveAdmin(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/query/for/search";
    }

    //跳转修改页面
    @RequestMapping("/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model){
        Admin admin = adminService.getAdminById(adminId);
        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN,admin);
        return "admin-edit";
    }

    //修改用户
    @RequestMapping("/update")
    public String updateAdmin(Admin admin,Integer pageNum){
        adminService.updateAdmin(admin);
        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }
}
